package com.secondGroup.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.secondGroup.entity.BankCard;
import com.secondGroup.entity.ElectronicAccount;
import com.secondGroup.entity.User;
import com.secondGroup.entity.VirtualBankCard;
import com.secondGroup.mapper.ElectronicAccountMapper;
import com.secondGroup.service.BankCardService;
import com.secondGroup.service.ElectronicAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.secondGroup.service.UserService;
import com.secondGroup.service.VirtualBankCardService;
import com.secondGroup.vo.StringVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Service

public class ElectronicAccountServiceImpl extends ServiceImpl<ElectronicAccountMapper, ElectronicAccount> implements ElectronicAccountService {
    @Autowired
    VirtualBankCardService virtualBankCardService;
    @Autowired
    UserService userService;
    @Autowired
    BankCardService bankCardService;
    @Override
    @Transactional
    public StringVo kaihu(String idNum,String eName, String payPassword, String cid, String bankName, HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        StringVo stringVo = new StringVo();
        VirtualBankCard virtualBankCard = virtualBankCardService.getOne(new QueryWrapper<VirtualBankCard>()
                .lambda()
                .eq(VirtualBankCard::getCId, cid)
                .eq(VirtualBankCard::getBank, bankName)
                .eq(VirtualBankCard::getIdNumber,idNum));
        if(virtualBankCard==null){
            stringVo.setI(0);
            stringVo.setS("查询不到此卡");
            return stringVo;
        }

        ElectronicAccount electronicAccount = new ElectronicAccount();

        electronicAccount.setAccount(user.getAccount());
        String eid = IdWorker.getIdStr();
        electronicAccount.setEId(eid);
        electronicAccount.setEName(eName);
        electronicAccount.setEPayPassword(payPassword);
        electronicAccount.setEMoney(new BigDecimal(0));
        boolean save = save(electronicAccount);
        boolean update = userService.update(new UpdateWrapper<User>()
                .lambda()
                .set(User::getEId, eid)
                .eq(User::getAccount,user.getAccount()));
        BankCard bankCard = new BankCard();
        bankCard.setAccount(user.getAccount());
        bankCard.setCId(cid);
        bankCardService.insert(user.getAccount(),cid);
        if(save&&update){
            user.setEId(eid);
            session.setAttribute("userInfo",user);
            stringVo.setS("电子账户开通成功");
            stringVo.setI(1);
            return stringVo;
        }return stringVo;


    }
}
